package com.wileyma.apps.adbbt.server;

import java.io.IOException;
import java.io.ObjectInputStream.GetField;
import java.util.Vector;
import javax.bluetooth.*;

public class ADBBTServer 
{
	// private final String mGenericData = "My generic data as a string.";
	
    public static final Vector<RemoteDevice> devicesDiscovered = new Vector<RemoteDevice>();

    public static void main(String[] args)  
    {
    	try 
    	{
			getDevices();
		} 
    	catch (IOException e) 
    	{
			e.printStackTrace();
		} 
    	catch (InterruptedException e) 
    	{
			e.printStackTrace();
		}
    }
    
    public static void getDevices() throws IOException, InterruptedException
    {
    	final Object inquiryCompletedEvent = new Object();

        devicesDiscovered.clear();

        DiscoveryListener listener = new DiscoveryListener() {

            public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
                System.out.println("Device " + btDevice.getBluetoothAddress() + " found");
                devicesDiscovered.addElement(btDevice);
                try {
                    System.out.println("     name " + btDevice.getFriendlyName(false));
                } catch (IOException cantGetDeviceName) {
                }
            }

            public void inquiryCompleted(int discType) {
                System.out.println("Device Inquiry completed!");
                synchronized(inquiryCompletedEvent){
                    inquiryCompletedEvent.notifyAll();
                }
            }

            public void serviceSearchCompleted(int transID, int respCode) {
            }

            public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
            }
        };

        synchronized(inquiryCompletedEvent) {
            boolean started = LocalDevice.getLocalDevice().getDiscoveryAgent().startInquiry(DiscoveryAgent.GIAC, listener);
            if (started) {
                System.out.println("wait for device inquiry to complete...");
                inquiryCompletedEvent.wait();
                System.out.println(devicesDiscovered.size() +  " device(s) found");
            }
        }
    }
}

